﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;

namespace ZJGIS.ManageUser
{
    public static class XzqUtility
    {
        public static IFeatureClass 县界FeatureClass = null;
        public static IFeatureClass 乡镇界FeatureClass = null;
        public static IFeatureClass 县界概略FeatureClass = null;
        public static IFeatureClass 乡镇界概略FeatureClass = null;

        public static List<XZQ> 县界XZQ = null;
        public static List<XZQ> 乡镇界XZQ = null;
        public static List<XZQ> 县界概略XZQ = null;
        public static List<XZQ> 乡镇界概略XZQ = null;

        static XzqUtility()
        {
            //GetXzqFeatCls(XzqType.县界);
            //GetXzqFeatCls(XzqType.县界概略);
            //GetXzqFeatCls(XzqType.乡镇界概略);
            //GetXzqFeatCls(XzqType.乡镇界);
        }

        public static List<XZQ> GetAllXZQs(XzqType xzq)
        {
            if (xzq == XzqType.县界)
            {
                if (县界XZQ == null)
                {
                    if (县界FeatureClass == null)
                        县界FeatureClass = GetXzqFeatureClass(xzq);
                    县界XZQ = GetXZQs(县界FeatureClass);
                }
                return 县界XZQ;
            }
            else if (xzq == XzqType.乡镇界)
            {

                if (乡镇界XZQ == null)
                {
                    if (乡镇界FeatureClass == null)
                        乡镇界FeatureClass = GetXzqFeatureClass(xzq);
                    乡镇界XZQ = GetXZQs(乡镇界FeatureClass);
                }
                return 乡镇界XZQ;
            }
            else if (xzq == XzqType.县界概略)
            {
                if (县界概略XZQ == null)
                {
                    if (县界概略FeatureClass == null)
                        县界概略FeatureClass = GetXzqFeatureClass(xzq);
                    县界概略XZQ = GetXZQs(县界概略FeatureClass);
                }
                return 县界概略XZQ;
            }
            else if (xzq == XzqType.乡镇界概略)
            {

                if (乡镇界概略XZQ == null)
                {
                    if (乡镇界概略FeatureClass == null)
                        乡镇界概略FeatureClass = GetXzqFeatureClass(xzq);
                    乡镇界概略XZQ = GetXZQs(乡镇界概略FeatureClass);
                }
                return 乡镇界概略XZQ;
            }
            else return null;
        }

        private static List<XZQ> GetXZQs(IFeatureClass fclass)
        {
            List<XZQ> lstXzq = new List<XZQ>();

            if (fclass == null || fclass.Fields.FindField("名称") < 0) return null;

            IQueryFilter filter = new QueryFilterClass();
            IFeatureCursor pFeatureCursor = fclass.Search(filter, false);
            IFeature feature = pFeatureCursor.NextFeature();
            while (feature != null)
            {
                XZQ xzq = new XZQ();
                xzq.Geometry = feature.ShapeCopy as IPolygon;
                xzq.XZQName = feature.get_Value(feature.Fields.FindField("名称")) as string;
                lstXzq.Add(xzq);
                feature = pFeatureCursor.NextFeature();
            }
            return lstXzq;
        }

        public static IFeatureClass GetXzqFeatCls(XzqType xzq)
        {
            try
            {
                if (xzq == XzqType.县界)
                {
                    if (县界FeatureClass == null)
                        return 县界FeatureClass = GetXzqFeatureClass(xzq);
                    return 县界FeatureClass;
                }
                else if (xzq == XzqType.乡镇界)
                {
                    if (乡镇界FeatureClass == null)
                    {
                        return 乡镇界FeatureClass = GetXzqFeatureClass(xzq);
                    }
                    else
                        return 乡镇界FeatureClass;
                }
                else if (xzq == XzqType.县界概略)
                {
                    if (县界概略FeatureClass == null)
                    {
                        return 县界概略FeatureClass = GetXzqFeatureClass(xzq);
                    }
                    else
                        return 县界概略FeatureClass;
                }
                else if (xzq == XzqType.乡镇界概略)
                {

                    if (乡镇界概略FeatureClass == null)
                    {
                        return 乡镇界概略FeatureClass = GetXzqFeatureClass(xzq);
                    }
                    else
                        return 乡镇界概略FeatureClass;
                }
                else return null;
            }
            catch (System.Exception)
            {
                return null;
            }
        }

        private static IFeatureWorkspace pWS = null;
        private static IFeatureClass GetXzqFeatureClass(XzqType xzq)
        {
            if (pWS == null)
            {
                IWorkspaceFactory pFactory = new ShapefileWorkspaceFactoryClass();

                DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "\\Data");
                if (!dir.Exists)
                {
                    dir.Create();
                }
                pWS = pFactory.OpenFromFile(Application.StartupPath + "\\Data", 0) as IFeatureWorkspace;
            }
            IFeatureClass pFeatCls = null;
            try
            {
                pFeatCls = pWS.OpenFeatureClass(xzq.ToString());
            }
            catch (Exception ex)
            {
                //throw new Exception("未找到" + xzq.ToString() + "数据，请先配置！");
                MessageBox.Show("未找到" + xzq.ToString() + "数据，请先配置！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //return null ;
            }
            return pFeatCls;
        }


        public static List<XZQ> GetXzqsByGeom(XzqType xzqType, IGeometry pGeo)
        {
            List<XZQ> lstXzq = new List<XZQ>();
            IFeatureClass pFeatCls = GetXzqFeatCls(xzqType);
            if (pFeatCls == null || pFeatCls.Fields.FindField("名称") < 0) return null;

            ISpatialFilter pSpatialFilter = new SpatialFilterClass();
            pSpatialFilter.Geometry = pGeo;
            pSpatialFilter.GeometryField = "SHAPE";
            pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            IFeatureCursor pFeatureCursor = pFeatCls.Search(pSpatialFilter, false);
            IFeature feature = pFeatureCursor.NextFeature();
            while (feature != null)
            {
                XZQ xzq = new XZQ();
                xzq.Geometry = feature.ShapeCopy as IPolygon;
                xzq.XZQName = feature.get_Value(feature.Fields.FindField("名称")) as string;
                lstXzq.Add(xzq);
                feature = pFeatureCursor.NextFeature();
            }
            return lstXzq;
        }

        public static IPolygon GetXzqByName(XzqType xzq, string xzqName)
        {
            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.SubFields = "*";
            pQueryFilter.WhereClause = "名称='" + xzqName + "'";
            return UnionGeometry(GetXzqFeatCls(xzq), pQueryFilter);
        }

        public static IPolygon GetXzqByNameLike(XzqType xzq, string xzqName)
        {
            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.SubFields = "*";
            pQueryFilter.WhereClause = "名称 like '%" + xzqName + "%'";
            return UnionGeometry(GetXzqFeatCls(xzq), pQueryFilter);
        }

        private static IPolygon UnionGeometry(IFeatureClass pFeatCls, IQueryFilter pQueryFilter)
        {
            if (pFeatCls == null) return null;
            IFeatureCursor pFeatCur = pFeatCls.Search(pQueryFilter, false);

            IGeometryCollection pGeomBag = new GeometryBagClass();
            IFeature pFeature = pFeatCur.NextFeature();
            object Missing = Type.Missing;
            while (pFeature != null)
            {
                pGeomBag.AddGeometry(pFeature.ShapeCopy, ref Missing, ref Missing);
                pFeature = pFeatCur.NextFeature();
            }
            IPolygon pGeometry = new PolygonClass();
            ITopologicalOperator pTopo = pGeometry as ITopologicalOperator;
            pTopo.ConstructUnion(pGeomBag as IEnumGeometry);
            if (pGeometry.IsEmpty)
                return null;
            else
                return pGeometry;
        }

        public static XzqType GetXzqType(string strXzqType)
        {
            XzqType xzqType = XzqType.县界;
            try
            {
                if (strXzqType.EndsWith("县界"))
                {
                    xzqType = XzqType.县界;
                }
                else if (strXzqType.EndsWith("乡镇界"))
                {
                    xzqType = XzqType.乡镇界;
                }
                else if (strXzqType.EndsWith("县界概略"))
                {
                    xzqType = XzqType.县界概略;
                }
                else if (strXzqType.EndsWith("乡镇界概略"))
                {
                    xzqType = XzqType.乡镇界概略;
                }
            }
            catch (System.Exception)
            {
            }
            return xzqType;
        }

        public static XzqType GetXzqTypeByXzqName(string strXzqName)
        {
            IPolygon pPolygon = GetXzqByName(GetXzqType("县界"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return GetXzqType("县界");

            pPolygon = GetXzqByName(GetXzqType("乡镇界"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return GetXzqType("乡镇界");

            pPolygon = GetXzqByName(GetXzqType("县界概略"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return GetXzqType("县界概略");

            pPolygon = GetXzqByName(GetXzqType("乡镇界概略"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return GetXzqType("乡镇界概略");

            return GetXzqType("县界");
        }

        public static IPolygon GetPolygonByXzqName(string strXzqName)
        {
            IPolygon pPolygon = GetXzqByName(GetXzqType("县界"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return pPolygon;

            pPolygon = GetXzqByName(GetXzqType("乡镇界"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return pPolygon;

            pPolygon = GetXzqByName(GetXzqType("县界概略"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return pPolygon;

            pPolygon = GetXzqByName(GetXzqType("乡镇界概略"), strXzqName);
            if (pPolygon != null && !pPolygon.IsEmpty) return pPolygon;

            return pPolygon;
        }

        public static IPolygon GetSelectXZQRegionByLists(List<XZQ> xzqs, out string strName)
        {
            IPolygon pAllPolygon = new PolygonClass();
            IGeometryCollection pGeoCol = new GeometryBagClass();
            object Missing = Type.Missing;
            int ii = 0;
            ITopologicalOperator pTopo = pAllPolygon as ITopologicalOperator;
            strName = null;
            foreach (XZQ xzq in xzqs)
            {
                IPolygon polygon = xzq.Geometry;
                if (polygon == null) continue;
                pGeoCol.AddGeometry(polygon, ref Missing, ref Missing);
                if (ii == 0)
                    strName = xzq.XZQName;
                else
                    strName += "_" + xzq.XZQName;
                ii++;
            }
            if (pGeoCol.GeometryCount == 0) return pAllPolygon;
            pTopo.ConstructUnion(pGeoCol as IEnumGeometry);
            pTopo.Simplify();
            return pAllPolygon;
        }
    }
}
